//
//  WYCBillController.m
//  51还款
//
//  Created by 中企互联 on 2017/10/30.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCBillController.h"
#import "PictureRotate.h"
#import "CCPScrollView.h"
#import "CCZTrotingLabel.h"
#import "WYCMymessegeController.h"
#import "HWRollingView.h"
#import "WYCPictureModel.h"
#import "WYCBillModel.h"
#import "WYCHomeCell.h"
#import "WYCBillFrameModel.h"
#import "WYCSeeBillController.h"
#import "WYCHandBillController.h"
#import "WYCUserMessegeModel.h"
#import "WYCAddCreditController.h"

@interface WYCBillController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) CCZTrotingLabel *trotView;

@property(nonatomic,strong)NSMutableArray * imageArray;
/** 列表*/
@property(nonatomic,strong)UITableView * tableView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * billarray;

/** 图片*/
@property(nonatomic,strong)UIImageView * backimg;

/** 提示*/
@property(nonatomic,strong)UILabel * backLabel;

/** 按钮*/
@property(nonatomic,strong)UIButton * ImportButton;

/** view*/
@property(nonatomic,strong)UIView * billView;

/** 背景*/
@property(nonatomic,strong)UIButton * zView;
@property(nonatomic,strong)UIButton *  backbutton ;


/** view*/
@property(nonatomic,strong)UIView * ejectView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * showArray;

@property (nonatomic,strong) CCZTrotingLabel *label;


@end

@implementation WYCBillController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;
    //图片数组
     self.imageArray = [[NSMutableArray alloc] init];
    //列表数组
    self.billarray = [[NSMutableArray alloc] init];
    
    self.showArray = [[NSMutableArray alloc] init];
    
    [self setusermyMessage];
    //创建列表
    [self setupTableView];
    [self setupIMG];    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupCreditcardlist)];
    [self loadversion];
}
- (void)setusermyMessage{
    [self showLoadingWithMessage:@"加载中..."];
    
    NSString * str = [NSString stringWithFormat:@"%@token=%@",usermyMessage,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"-=----------%@",responseObject);
        if ([[responseObject objectForKey:@"code"]intValue] == 0) {
            [self hideLoading];
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCUserMessegeModel * usermodel = [WYCUserMessegeModel mj_objectWithKeyValues:dict];
                [self.showArray addObject:usermodel.content];
                
            }
//            [self.label addTextArray:self.showArray];
            [self shownotion];
            WYCLog(@"---------%@",self.showArray);
        }else{
            [self hideLoading];
            [self showToast:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
#pragma mark ------>>创建tableview
- (void)setupTableView{
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.estimatedSectionHeaderHeight=0;
    
    self.tableView.estimatedSectionFooterHeight=0;
    int a = 730 * px;
    int b = NavHeight;
    WYCLog(@"高度---%d",a);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, a + b, ScreenWidth, ScreenHeight - (a + b)) style:UITableViewStylePlain];
    WYCLog(@"高度---%f",self.tableView.y);
    
    WYCLog(@"-%d",a + NavHeight);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    
}

#pragma make ------->>>列表请求
- (void)setupCreditcardlist{
    
    [self.billarray removeAllObjects];
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            if (resultArray.count == 0) {
                WYCBillModel * model = [[WYCBillModel alloc] init];
                model.userId = @"-1";
                model.bankName = @"示例卡";
                model.lastNumber = @"8888";
                model.name = @"李小明";
                model.outbBllDay = @"0";
                model.billDate = @"20180106";
                model.dueDate = @"20180125";
                model.bankId = @"-1";
                model.approve = @"-1";
                model.isHideCard = @"0";
                WYCBillFrameModel * BillFrameModel = [[WYCBillFrameModel alloc] init];
                BillFrameModel.Billmodel = model;
                [self.billarray addObject:BillFrameModel];
            }else{
            for (NSDictionary * dict in resultArray) {
                
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{
                WYCBillFrameModel * BillFrameModel = [[WYCBillFrameModel alloc] init];
                BillFrameModel.Billmodel = model;
                [self.billarray addObject:BillFrameModel];
            
                }
            }
                
            }
            WYCBillModel * model = [[WYCBillModel alloc] init];
            model.bankId = @"-2";
            model.addcard = @" +添加信用卡";
            model.isHideCard = @"0";
            model.approve = @"-2";
            model.userId = @"-2";
            WYCBillFrameModel * BillFrameModel = [[WYCBillFrameModel alloc] init];
            BillFrameModel.Billmodel = model;
            [self.billarray addObject:BillFrameModel];
            WYCLog(@"银行id----多少%@",model.addcard);
        }else{
            
            [self showToast:msgStr];
            
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self showToast:Failed];
    }];
    
}
- (void)ObtainButton{
    
    self.billView = [[UIView alloc] init];
    self.billView.frame = CGRectMake(ScreenWidth / 3 - 50 * px, ScreenHeight / 2 + 200 * px, ScreenWidth / 3 + 100 * px, ScreenWidth / 3);
    self.billView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:self.billView];
    
    _backimg = [[UIImageView alloc] init];
    _backimg.image = [UIImage imageNamed:@"home_photo"];
    _backimg.frame = CGRectMake(0, 0, self.billView.width / 2 - 103 * px,225 * px);
    [self.billView addSubview:_backimg];
    
    
    _backLabel = [[UILabel alloc] init];
    _backLabel.text = @"还没有账单信息哟~";
    _backLabel.textAlignment = NSTextAlignmentCenter;
    _backLabel.font = [UIFont systemFontOfSize:SmallFont];
    _backLabel.textColor = WYCRGBColor(186, 186, 186);
    _backLabel.frame = CGRectMake(_backimg.x,CGRectGetMaxY(_backimg.frame) + 10 * px,_backimg.width,48 * px);
    [self.billView addSubview:_backLabel];
    
    
    _ImportButton = [[UIButton alloc] init];
    _ImportButton.frame = CGRectMake(_backLabel.x + 50 * px, CGRectGetMaxY(_backLabel.frame) + 80 * px, _backLabel.width - 100 * px , 100 * px);
    [_ImportButton setTitle:@"导入账单" forState:UIControlStateNormal];
    [_ImportButton addTarget:self action:@selector(importClick) forControlEvents:UIControlEventTouchUpInside];
    _ImportButton.backgroundColor = [UIColor whiteColor];
    _ImportButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [_ImportButton setTitleColor:WYCRGBColor(195, 195, 195) forState:UIControlStateNormal];
    [self.billView addSubview:_ImportButton];
}
#pragma make ------->>>轮播图
- (void)setupIMG{
    
    [HttpTool POST:bannerlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSLog(@"%@",responseObject);
            NSArray * resultarray = responseObject[@"result"];
            for (NSDictionary * dict in resultarray) {
                
                WYCPictureModel * model = [WYCPictureModel mj_objectWithKeyValues:dict];
                [self.imageArray addObject:model.imgUrl];
            }
            [self initImageNamesSystemPageControlAndTitle];
            
        }else{
            
            [self showToast:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
}

- (void)shownotion{
    
    int a = NavHeight;
    //通知的View
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(0, 550 * px + a, ScreenWidth, WYCcellHeight);
    showView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:showView];
    
    //通知imageView
    UIImageView *noticeimageView = [[UIImageView alloc] init];
    noticeimageView.frame = CGRectMake(80 * px, 60 * px, 40 * px, 40 * px);
    //    noticeimageView.backgroundColor = [UIColor blackColor];
    noticeimageView.image = [UIImage imageNamed:@"laba"];
    [showView addSubview:noticeimageView];
    
    
    UIView * duanView = [[UIView alloc] init];
    duanView.backgroundColor = WYCRGBColor(205, 205, 205);
    duanView.frame = CGRectMake(CGRectGetMaxX(noticeimageView.frame) + 40 * px, 20 * px, 0.5, showView.height - 40 * px);
    [showView addSubview:duanView];
    
    self.label = [[CCZTrotingLabel alloc] init];
    self.label.frame = CGRectMake(CGRectGetMaxX(duanView.frame) + 40 * px, 30 * px, ScreenWidth - noticeimageView.width - 120 * px, 80 * px);
    self.label.direction = CCZTrotDirectionTop;
    self.trotView = self.label;
    self.label.repeatTextArr = YES;
    self.label.hideWhenStopTroting = YES;
    self.label.pause = 4;
    [self.label addTextArray:self.showArray];
//    self.label.backgroundImage = [UIImage imageNamed:@"1"];
    [showView addSubview:self.label];
}
/**
 *  使用本地图片、系统PageControl、设置标题
 */
- (void)initImageNamesSystemPageControlAndTitle {
    
    UIView * cellview = [[UIView alloc] init];
    cellview.frame = CGRectMake(0, NavHeight, ScreenWidth, 550  * px);
    cellview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:cellview];
    
    
    HWRollingView * RollingView = [HWRollingView rollingViewWithFrame:CGRectMake(0, 0, ScreenWidth, 550 * px) RollingStyle:RollingStyleNetwork Data:self.imageArray];
    [cellview addSubview:RollingView];

}
- (void)importClick{
    [self addClick];
    
}


#pragma make ------>>右侧按钮 点击事件
- (void)addClick{
    
    UIView * ejectView = [[UIView alloc] init];
    ejectView.frame = CGRectMake(0, ScreenHeight - 500 * px, ScreenWidth, 500 * px);
    ejectView.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication].keyWindow  addSubview:ejectView];
    self.ejectView = ejectView;
    
    
    UIButton * zView = [[UIButton alloc] init];
    zView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - ejectView.height);
    zView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [zView addTarget:self action:@selector(zviewClick) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow  addSubview:zView];
    self.zView = zView;
    
    
    
    UIView * mallLine = [[UIView alloc] init];
    mallLine.backgroundColor = [UIColor grayColor];
//    mallLine.frame = CGRectMake(0, CGRectGetMaxY(maillButton.frame) + 25 * px, ejectView.width, 0.5);
    [ejectView addSubview:mallLine];
    
    
    UIButton * holdButton = [[UIButton alloc] init];
    holdButton.frame = CGRectMake(0, CGRectGetMaxY(mallLine.frame) + 25 * px, ejectView.width, 100 * px);
    [holdButton setTitle:@"手输账单" forState:UIControlStateNormal];
    holdButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [holdButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    holdButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [holdButton addTarget:self action:@selector(holdButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ejectView addSubview:holdButton];
    
    
    UIView * line = [[UIView alloc] init];
    line.backgroundColor = WYCGlobalBg;
    line.frame = CGRectMake(0, CGRectGetMaxY(holdButton.frame) + 25 * px, ejectView.width, 20 * px);
    [ejectView addSubview:line];
    
    
    UIButton * NoButton = [[UIButton alloc] init];
    NoButton.frame = CGRectMake(0, CGRectGetMaxY(line.frame) + 25 * px, ejectView.width, 100 * px);
    [NoButton setTitle:@"取消" forState:UIControlStateNormal];
    NoButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [NoButton addTarget:self action:@selector(NoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [NoButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    NoButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [ejectView addSubview:NoButton];
    
}
- (void)maillButtonClick{
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    [self showToast:@"开发中..."];
    
}

- (void)holdButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    WYCHandBillController * handbill = [[WYCHandBillController alloc] init];
    [self.navigationController pushViewController:handbill animated:NO];
}

- (void)NoButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
- (void)zviewClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
#pragma make ------>>左侧按钮 点击事件
- (void)photoClick{
    WYCMymessegeController * mymessege = [[WYCMymessegeController alloc] init];
    [self.navigationController pushViewController:mymessege animated:NO];
}


#pragma make ------>>最上侧轮播图
- (void)setupScrollView {
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.billarray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WYCBillFrameModel * model = self.billarray[indexPath.row];
    
    return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WYCHomeCell * cell = [WYCHomeCell cellWithTableView:tableView];
    cell.billmodelFrame = self.billarray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if ([cell.repaymentButton.titleLabel.text isEqualToString:@"立即认证"]) {
        [cell.repaymentButton addTarget:self action:@selector(repayMentClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYCBillFrameModel * frameModel = self.billarray[indexPath.row];
    
    if ([frameModel.Billmodel.approve isEqualToString:@"2"] || [frameModel.Billmodel.userId isEqualToString:@"-1"]) {
        
        return;
    }else if ([frameModel.Billmodel.userId isEqualToString:@"-2"]){
        
        WYCAddCreditController * addcard = [[WYCAddCreditController alloc] init];
        [self.navigationController pushViewController:addcard animated:NO];
    }
    else{
        
        WYCSeeBillController * SeeBill = [[WYCSeeBillController alloc] init];
        //银行卡账号
        SeeBill.cardId = frameModel.Billmodel.cardId;

        //银行卡姓名
        SeeBill.bankName = frameModel.Billmodel.bankName;

        //银行卡后四位
        SeeBill.lastNumber = frameModel.Billmodel.lastNumber;

        //银行卡多少天出账单
        SeeBill.outbBllDay = frameModel.Billmodel.outbBllDay;

        //本期账单金额
        SeeBill.billAmount = frameModel.Billmodel.billAmount;

        //认证状态
        SeeBill.state = frameModel.Billmodel.state;
        
        //免息期
        SeeBill.gracePeriod = frameModel.Billmodel.gracePeriod;


        //总额度
        SeeBill.creditLine = frameModel.Billmodel.creditLine;


        //还款日
        SeeBill.dueDate = frameModel.Billmodel.dueDate;


        //账单日
        SeeBill.billDate = frameModel.Billmodel.billDate;


        SeeBill.userCard = frameModel.Billmodel.userCard;

        [self.navigationController pushViewController:SeeBill animated:NO];
    }
}

- (void)repayMentClick{
    
    [self showToast:@"绑定成功！请耐心等待认证结果或拨打客服电话咨询"];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    [self.tableView.mj_header beginRefreshing];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    //返回按钮
    _backbutton= [[UIButton alloc] init];
    [_backbutton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [_backbutton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    self.backbutton.hidden =YES;

    _backbutton.frame = CGRectMake(NavView.width - 96 * px, titleY, 70 * px , 70 * px);
    [NavView addSubview:_backbutton];

    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"ring"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"51HK";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loadversion
{
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"=======%@",appVersion);
    params[@"version"] = appVersion;
    
    [HttpTool POST:function params:params success:^(NSURLSessionDataTask *task, id responseObject) {
       if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
           NSLog(@"第一%@",responseObject);
           if ([[responseObject objectForKey:@"result"] intValue ] == 0) {
               self.backbutton.hidden =YES;
           }
           else
           {
                self.backbutton.hidden =NO;
           }
  
        }
       

    } fail:^(NSURLSessionDataTask *task, NSError *error) {

        [self showToast:Failed];
    }];
}
@end
